<?php
namespace Admin\Controller;
use Think\Controller;
class MoneyController extends CommonController{
    public function index(){

    	$page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
        if($_GET['user_name']){
            $where['b.username'] = $_GET['user_name'];
        }
       	$data = M('money_log a')
       			->field("a.id,a.user_id,a.money,a.add_time,a.type,a.content,a.pay_type,b.username")
       			->page($page_no, $page_size)
                ->where('a.money != 0')
       			->join("left join zm_user b on a.user_id=b.id")
                ->where($where)
       			->select();
        
        foreach ($data as $k => $v) 
        {
            if($v['type'] == 101 || $v['type'] == 102){
                $data[$k]['money'] = '-'.$v['money'];
            }else{
                $data[$k]['money'] = '+'.$v['money'];
            }

            if($v['pay_type'] == 0){
                $data[$k]['pay_type'] = '余额';
            }elseif ($v['pay_type'] == 1) {
                $data[$k]['pay_type'] = '微信';    
            }elseif ($v['pay_type'] == 2) {
                $data[$k]['pay_type'] = '余额+微信';    
            }else{
                $data[$k]['pay_type'] = '其他';    
            }
            if($v['type'] == 101){
                //type 101提现
                $data[$k]['title'] = '余额提现';
            }elseif ($v['type'] == 102) {
                $themes = explode(',',$v['theme']);
                $title = ""; $cnt = count($themes);
                if ($themes[0] == 1) {
                    if ($cnt == 1) {
                        $title = "发起美食活动";
                    }elseif ($cnt == 2) {
                        if ($themes[1]==2) {$title = "发起美食&middot;K歌活动";} else{$title = "发起美食/&middot;酒吧活动";}
                    }else{
                        $title = "发起美食&middot;K歌&middot;酒吧活动";
                    }
                }elseif ($themes[0] == 2) {
                    if ($cnt == 1) {$title = "发起K歌活动";} else{$title = "发起K歌&middot;酒吧活动";}
                }else{
                    $title = "发起酒吧活动";
                }
                //type 102消费
                if($v['pay_type'] == 0){
                    //余额
                    $data[$k]['title'] = $title.'&nbsp;(余额)';
                }elseif ($v['pay_type'] == 1) {
                    //微信
                    $data[$k]['title'] = $title.'&nbsp;(微信)';
                }elseif ($v['pay_type'] == 2) {
                    //余额+微信
                    $data[$k]['title'] = $title.'&nbsp;(余额+微信)';
                }elseif ($v['pay_type'] == 3) {
                    //其他
                    $data[$k]['title'] = $title;
                }
            }elseif ($v['type'] == 103) {
                //type 103充值
                $data[$k]['title'] = '充值';
            }elseif ($v['type'] == 104) {
                //type 104收到红包
                $data[$k]['title'] = '红包兑换成现金余额';
            }elseif ($v['type'] == 105) {
                //type 105红包退回
                //type 106 活动过期退款
                $themes = explode(',',$v['theme']);
                $title = ""; $cnt = count($themes);
                if ($themes[0] == 1) {
                    if ($cnt == 1) {
                        $title = "美食活动受邀者爽约退款";
                    }elseif ($cnt == 2) {
                        if ($themes[1]==2) {
                            $title = "美食&middot;K歌活动受邀者爽约退款";
                        } else{
                            $title = "美食/&middot;酒吧活动受邀者爽约退款";
                        }
                    }else{
                        $title = "美食&middot;K歌&middot;酒吧活动受邀者爽约退款";
                    }
                }elseif ($themes[0] == 2) {
                    if ($cnt == 1) {$title = "K歌活动受邀者爽约退款";} else{$title = "K歌&middot;酒吧活动受邀者爽约退款";}
                }else{
                    $title = "酒吧活动受邀者爽约退款";
                }
                $data[$k]['title'] = $title;
            }elseif($v['type'] == 106){
                //type 106 活动过期退款
                $themes = explode(',',$v['theme']);
                $title = ""; $cnt = count($themes);
                if ($themes[0] == 1) {
                    if ($cnt == 1) {
                        $title = "美食活动过期退款";
                    }elseif ($cnt == 2) {
                        if ($themes[1]==2) {
                            $title = "美食&middot;K歌活动过期退款";
                        } else{
                            $title = "美食/&middot;酒吧活动过期退款";
                        }
                    }else{
                        $title = "美食&middot;K歌&middot;酒吧活动过期退款";
                    }
                }elseif ($themes[0] == 2) {
                    if ($cnt == 1) {$title = "K歌活动过期退款";} else{$title = "K歌&middot;酒吧活动过期退款";}
                }else{
                    $title = "酒吧活动过期退款";
                }
                $data[$k]['title'] = $title;
            }elseif($v['type'] == 107){
                //type 107 活动取消退款
                $themes = explode(',',$v['theme']);
                $title = ""; $cnt = count($themes);
                if ($themes[0] == 1) {
                    if ($cnt == 1) {
                        $title = "美食活动取消退款";
                    }elseif ($cnt == 2) {
                        if ($themes[1]==2) {
                            $title = "美食&middot;K歌活动取消退款";
                        } else{
                            $title = "美食/&middot;酒吧活动取消退款";
                        }
                    }else{
                        $title = "美食&middot;K歌&middot;酒吧活动取消退款";
                    }
                }elseif ($themes[0] == 2) {
                    if ($cnt == 1) {$title = "K歌活动取消退款";} else{$title = "K歌&middot;酒吧活动取消退款";}
                }else{
                    $title = "酒吧活动取消退款";
                }
                $data[$k]['title'] = $title;
            }else{
                $data[$k]['title'] = '支付诚意金';
            }
        }
      
       	$count = M("money_log a")->join("left join zm_user b on a.user_id=b.id")->where($where)->count();
       	$Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign("count", $count);
       	$this->assign('data',$data);
        $this->display();
    }

    public function user_record(){
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
        $user_id = $_GET['user_id'] ? $_GET['user_id'] : 0;
        if($user_id){
            $where['a.user_id'] = $user_id;
        }
        $where['a.money'] = array('neq',0);
        $data = M('money_log a')
                ->field("a.id,a.user_id,a.money,a.add_time,a.type,a.content,a.pay_type,b.username")
                ->page($page_no, $page_size)
                ->where($where)
                ->join("left join zm_user b on a.user_id=b.id")
                ->select();
        
        foreach ($data as $k => $v) 
        {
            if($v['type'] == 101 || $v['type'] == 102){
                $data[$k]['money'] = '-'.$v['money'];
            }else{
                $data[$k]['money'] = '+'.$v['money'];
            }

            if($v['pay_type'] == 0){
                $data[$k]['pay_type'] = '余额';
            }elseif ($v['pay_type'] == 1) {
                $data[$k]['pay_type'] = '微信';    
            }elseif ($v['pay_type'] == 2) {
                $data[$k]['pay_type'] = '余额+微信';    
            }else{
                $data[$k]['pay_type'] = '其他';    
            }
            if($v['type'] == 101){
                //type 101提现
                $data[$k]['title'] = '余额提现';
            }elseif ($v['type'] == 102) {
                $themes = explode(',',$v['theme']);
                $title = ""; $cnt = count($themes);
                if ($themes[0] == 1) {
                    if ($cnt == 1) {
                        $title = "发起美食活动";
                    }elseif ($cnt == 2) {
                        if ($themes[1]==2) {$title = "发起美食&middot;K歌活动";} else{$title = "发起美食/&middot;酒吧活动";}
                    }else{
                        $title = "发起美食&middot;K歌&middot;酒吧活动";
                    }
                }elseif ($themes[0] == 2) {
                    if ($cnt == 1) {$title = "发起K歌活动";} else{$title = "发起K歌&middot;酒吧活动";}
                }else{
                    $title = "发起酒吧活动";
                }
                //type 102消费
                if($v['pay_type'] == 0){
                    //余额
                    $data[$k]['title'] = $title.'&nbsp;(余额)';
                }elseif ($v['pay_type'] == 1) {
                    //微信
                    $data[$k]['title'] = $title.'&nbsp;(微信)';
                }elseif ($v['pay_type'] == 2) {
                    //余额+微信
                    $data[$k]['title'] = $title.'&nbsp;(余额+微信)';
                }elseif ($v['pay_type'] == 3) {
                    //其他
                    $data[$k]['title'] = $title;
                }
            }elseif ($v['type'] == 103) {
                //type 103充值
                $data[$k]['title'] = '充值';
            }elseif ($v['type'] == 104) {
                //type 104收到红包
                $data[$k]['title'] = '红包兑换成现金余额';
            }elseif ($v['type'] == 105) {
                //type 105红包退回
                //type 106 活动过期退款
                $themes = explode(',',$v['theme']);
                $title = ""; $cnt = count($themes);
                if ($themes[0] == 1) {
                    if ($cnt == 1) {
                        $title = "美食活动受邀者爽约退款";
                    }elseif ($cnt == 2) {
                        if ($themes[1]==2) {
                            $title = "美食&middot;K歌活动受邀者爽约退款";
                        } else{
                            $title = "美食/&middot;酒吧活动受邀者爽约退款";
                        }
                    }else{
                        $title = "美食&middot;K歌&middot;酒吧活动受邀者爽约退款";
                    }
                }elseif ($themes[0] == 2) {
                    if ($cnt == 1) {$title = "K歌活动受邀者爽约退款";} else{$title = "K歌&middot;酒吧活动受邀者爽约退款";}
                }else{
                    $title = "酒吧活动受邀者爽约退款";
                }
                $data[$k]['title'] = $title;
            }elseif($v['type'] == 106){
                //type 106 活动过期退款
                $themes = explode(',',$v['theme']);
                $title = ""; $cnt = count($themes);
                if ($themes[0] == 1) {
                    if ($cnt == 1) {
                        $title = "美食活动过期退款";
                    }elseif ($cnt == 2) {
                        if ($themes[1]==2) {
                            $title = "美食&middot;K歌活动过期退款";
                        } else{
                            $title = "美食/&middot;酒吧活动过期退款";
                        }
                    }else{
                        $title = "美食&middot;K歌&middot;酒吧活动过期退款";
                    }
                }elseif ($themes[0] == 2) {
                    if ($cnt == 1) {$title = "K歌活动过期退款";} else{$title = "K歌&middot;酒吧活动过期退款";}
                }else{
                    $title = "酒吧活动过期退款";
                }
                $data[$k]['title'] = $title;
            }elseif($v['type'] == 107){
                //type 107 活动取消退款
                $themes = explode(',',$v['theme']);
                $title = ""; $cnt = count($themes);
                if ($themes[0] == 1) {
                    if ($cnt == 1) {
                        $title = "美食活动取消退款";
                    }elseif ($cnt == 2) {
                        if ($themes[1]==2) {
                            $title = "美食&middot;K歌活动取消退款";
                        } else{
                            $title = "美食/&middot;酒吧活动取消退款";
                        }
                    }else{
                        $title = "美食&middot;K歌&middot;酒吧活动取消退款";
                    }
                }elseif ($themes[0] == 2) {
                    if ($cnt == 1) {$title = "K歌活动取消退款";} else{$title = "K歌&middot;酒吧活动取消退款";}
                }else{
                    $title = "酒吧活动取消退款";
                }
                $data[$k]['title'] = $title;
            }else{
                $data[$k]['title'] = '支付诚意金';
            }
        }
      
        $count = M("money_log a")->where($where)->count();
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign("count", $count);
        $this->assign('data',$data);
        $this->display();
    }
    
}
